//
//  HomeCtrl.m
//  Traveling
//
//  Created by Northwan on 15/5/12.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//  首页

#import "HomeCtrl.h"
#import "TravelineCtrl.h"
#import "TicketSelectView.h"
#import "StayHotelCtrl.h"
#import "ScenicoListCtrl.h"
#import "TraveSpecialtyCtrl.h"
#import "CustomShowView.h"
#import "PublicWebViewCtrl.h"
#import "TicketInfoCtrl.h"
#import "ScenicoNavCtrl.h"
#import "ZBarViewCtrl.h"
#import "HomeDataTool.h"
#import "ImagePlayerView.h"
#import "SGFocusImageItem.h"
#import "SGFocusImageFrame.h"
#import "CityList.h"
#import "HomeSearchView.h"
#import <BaiduMapAPI/BMapKit.h>

@interface HomeCtrl ()<TicketSelectViewDelegate,ImagePlayerViewDelegate,UIScrollViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic ,weak) CustomShowView *showView;
@property (nonatomic, strong) BannerRes *bannerRes;
@property (weak, nonatomic) IBOutlet UIImageView *bannerImage;
@property(nonatomic,strong)BMKLocationService *locService;
@end

@implementation HomeCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"黄陂智慧旅游"];
    [self.scrollView setAlwaysBounceVertical:YES];
    self.scrollView.delegate = self;
    [self requestData];
    
    [self initBaiduMapLocation];
}

-(void)initBaiduMapLocation{
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    [BMKLocationService setLocationDistanceFilter:100.f];
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    [_locService startUserLocationService];
}

-(void)requestData{
    [HomeDataTool getHomeAdvList:^(BannerRes *result) {
        if (result.isSucc) {
            self.bannerRes = result;
            ImagePlayerView *imagePlayerView = [[ImagePlayerView alloc] initWithFrame:self.bannerImage.bounds];
            [imagePlayerView initWithCount:result.datas.count delegate:self];
            [self.bannerImage addSubview:imagePlayerView];
        }
    } failure:^(NSError *error) {
    }];
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index{
    [imageView sd_setImageWithURL:[NSURL URLWithString:[self.bannerRes.datas[index] imgurl]]];
}

- (IBAction)onItemTap:(UIButton *)sender {
    switch (sender.tag) {
        case 0:{
            NSLog(@"虚拟景区");
            PublicWebViewCtrl *ctrl = [[PublicWebViewCtrl alloc] initWithUrl:[NSString stringWithFormat:@"%@%@",BASE_URL,@"scenic/v_scenic.rhtml"]];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case 1:{
            NSLog(@"景区导航");
            ScenicoNavCtrl *ctrl = [[ScenicoNavCtrl alloc] init];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case 2:{
            NSLog(@"景点景区");
            ScenicoListCtrl *ctrl = [[ScenicoListCtrl alloc] init];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case 3:{
            NSLog(@"旅游线路");
            TravelineCtrl *ctrl = [[TravelineCtrl alloc] init];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case 4:{
            NSLog(@"旅游特产");
            TraveSpecialtyCtrl *ctrl = [[TraveSpecialtyCtrl alloc] init];
            [self.navigationController pushViewController:ctrl animated:YES];
            break;
        }
        case 5:{
            NSLog(@"酒店住宿");
            StayHotelCtrl *ctrl =  [[StayHotelCtrl alloc]init];
            [self.navigationController pushViewController:ctrl animated:YES];
             break;
        }
           
        default:
            break;
    }
}

- (IBAction)titleItemTap:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            NSLog(@"扫一扫");
            [self.navigationController pushViewController:[[ZBarViewCtrl alloc] init] animated:YES];
            break;
        case 1:
            [self searchDialog];
            //搜索
            break;
        case 2:
            NSLog(@"票务信息");
            [self ticketClick:sender];
            break;
        default:
            break;
    }
}

-(void)searchDialog{
    HomeSearchView *searchView = [[HomeSearchView alloc] initWithBlock:^(NSString *text) {
        [self.showView hideWithanim:YES];
        [self.navigationController pushViewController:[[ScenicoListCtrl alloc] initWithKeyWord:text] animated:YES];
    }];
    CustomShowView *showView = [[CustomShowView alloc] initWithCustomView:searchView leftTopPoint:CGPointMake(0, 20)];
    showView.backgroundColor = RGBAColor(0, 0, 0, 0.3);
    [showView showWithanim:YES];
    self.showView = showView;
}

-(void)ticketClick:(UIButton *)button{
    TicketSelectView *ticket = [TicketSelectView ticketView];
    ticket.width = button.superview.width-button.x;
    ticket.delegate = self;
    //获取button在屏幕 上的位置
    CGRect rect = [button.superview convertRect:button.frame toView:nil];
    CustomShowView *showView = [[CustomShowView alloc] initWithCustomView:ticket leftTopPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect))];
    [showView showWithanim:YES];
    self.showView = showView;
}

-(void)ticketBtnClick:(TicketSelectView *)ticketView type:(TicketSelectViewClickType)type{
    if (type==TicketSelectViewClickTypeAir) {
        //飞机
        NSLog(@"飞机");
        [self.navigationController pushViewController:[[TicketInfoCtrl alloc]initWithType:TicketSelectViewClickTypeAir] animated:YES];
    }else if (type == TicketSelectViewClickTypeTrain){
        //火车
        NSLog(@"火车");
         [self.navigationController pushViewController:[[TicketInfoCtrl alloc]initWithType:TicketSelectViewClickTypeTrain] animated:YES];
    }else{
        //汽车
        NSLog(@"汽车");
         [self.navigationController pushViewController:[[TicketInfoCtrl alloc]initWithType:TicketSelectViewClickTypeCar] animated:YES];
    }
    [self.showView hideWithanim:YES];
    self.showView = nil;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}


- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    if(userLocation.location){
        NSLog(@"\n百度到位置:\n  纬度lat:%f\n  经度lng:%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
        BMKGeoCodeSearch *geoCodeSearch = [[BMKGeoCodeSearch alloc]init];
        geoCodeSearch.delegate  = self;
        BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc]init];
        reverseGeoCodeOption.reverseGeoPoint = userLocation.location.coordinate;
        [geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
        [_locService stopUserLocationService];
    }
}

-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    NSString* city =  result.addressDetail.city;
    [DefaultsHelper setObject:city forKey:k_LocationAddressCity];
    [self.navigationItem.leftBarButtonItem setTitle:city];
    NSLog(@"%@",city);
}

#pragma mark - searchbardelegate
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.navigationController pushViewController:[[ScenicoListCtrl alloc] initWithKeyWord:searchBar.text] animated:YES];
    searchBar.text = @"";
}

@end
